﻿using Abp.Domain.Entities;
using Abp.Domain.Repositories;
using Abp.Domain.Services;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Security.Cryptography;
using Teleware.Newbe.Navigation.Web.Domain.Entities;

namespace Teleware.Newbe.Navigation.Web.Domain.Services.Impl
{
    public class DataInitService : DomainService, IDataInitService
    {
        private readonly IRepository<Department, string> _deptRepository;
        private readonly IRepository<Account, long> _accounts;
        private readonly IRepository<HrefInfo, string> _hrefInfos;
        private readonly IRepository<InfoTag, string> _tagsRepository;
        private readonly IRepository<HrefInfoTag> _hreInfoTagRepository;

        public DataInitService(
            IRepository<Department, string> deptRepository,
            IRepository<Account, long> accounts,
            IRepository<InfoTag, string> tagsRepository,
            IRepository<HrefInfo, string> hrefInfos,
            IRepository<HrefInfoTag> hreInfoTagRepository)
        {
            _deptRepository = deptRepository;
            _accounts = accounts;
            _hrefInfos = hrefInfos;
            _tagsRepository = tagsRepository;
            _hreInfoTagRepository = hreInfoTagRepository;
        }

        public void Init()
        {
            var yanfa = new Department
            {
                Id = "yanfabu".GetMd5Id(),
                Name = "研发部",
                OrderNo = "0",
            };
            var depts = new List<Department>
            {
                yanfa,
                new Department
                {
                    Id = "oa".GetMd5Id(),
                    Name = "OA管理部",
                    OrderNo = "0",
                },
                new Department
                {
                    Id = "xinxizhongxin".GetMd5Id(),
                    Name = "信息中心",
                    OrderNo = "0",
                },
            };
            foreach (var department in depts)
            {
                _deptRepository.InsertIfNotExsisted(department);
            }
            var yukun = new Account
            {
                UserId = "yuk",
                Id = 999,
                Name = "俞坤",
                DepartmentId = yanfa.Id,
            };
            var accounts = new List<Account>
            {
                yukun
            };
            accounts.ForEach(account =>
            {
                var acc = _accounts.FirstOrDefault(x => x.UserId == account.UserId);
                if (acc == null)
                {
                    _accounts.Insert(account);
                }
            });
            yukun = _accounts.FirstOrDefault(x => x.UserId == yukun.UserId);
            var now = DateTime.Now;
            var neiwangTag = new InfoTag
            {
                Id = "lianjie".GetMd5Id(),
                CreatorUserId = yukun.Id,
                CreationTime = now,
                OrderNo = "0",
                Text = "内网",
            };
            _tagsRepository.InsertIfNotExsisted(neiwangTag);
            var hrefInfo = new HrefInfo
            {
                Id = "baidu".GetMd5Id(),
                DepartmentId = yanfa.Id,
                CreatorUserId = yukun.Id,
                CreationTime = now,
                Name = "百度",
                Remark = "百度一下你就知道",
                Href = "http://www.baidu.com",
                OrderNo = "0",
            };
            _hrefInfos.InsertIfNotExsisted(hrefInfo);
            _hreInfoTagRepository.InsertIfNotExsisted((x) => x.InfoTagId == neiwangTag.Id && x.HrefInfoId == hrefInfo.Id
                , new HrefInfoTag
                {
                    InfoTagId = neiwangTag.Id,
                    HrefInfoId = hrefInfo.Id,
                });

            var yukunqq = new HrefInfo
            {
                Id = "yukunqq".GetMd5Id(),
                DepartmentId = yanfa.Id,
                CreatorUserId = yukun.Id,
                CreationTime = now,
                Name = "俞坤",
                Href = "http://www.baidu.com",
                Remark = "研发部成员",
                Content = "472158246",
                OrderNo = "0",
            };
            _hrefInfos.InsertIfNotExsisted(yukunqq);
            var chenbinqq = new HrefInfo
            {
                Id = "chenbinqq".GetMd5Id(),
                DepartmentId = yanfa.Id,
                CreatorUserId = yukun.Id,
                CreationTime = now,
                Name = "陈斌",
                Href = "http://www.baidu.com",
                Remark = "特力惠唯一指定大佬",
                Content = "2196418440",
                OrderNo = "0",
            };
            _hrefInfos.InsertIfNotExsisted(chenbinqq);
            var tmpqqGroup = new HrefInfo
            {
                Id = "tmpqqGroup".GetMd5Id(),
                DepartmentId = yanfa.Id,
                CreatorUserId = yukun.Id,
                CreationTime = now,
                Name = "TMP技术支持",
                Remark = "TMP技术支持",
                Content = "615657343",
                Href = "https://jq.qq.com/?_wv=1027&k=5gnUjju",
                OrderNo = "0",
            };
            _hrefInfos.InsertIfNotExsisted(tmpqqGroup);
        }
    }

    internal static class RepostryExtensions
    {
        public static TEntity InsertIfNotExsisted<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repo,
            Expression<Func<TEntity, bool>> condition,
            TEntity entity) where TEntity : class, IEntity<TPrimaryKey>
        {
            if (repo.FirstOrDefault(condition) == null)
            {
                return repo.Insert(entity);
            }
            return entity;
        }

        public static TEntity InsertIfNotExsisted<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repo,
            TEntity entity) where TEntity : class, IEntity<TPrimaryKey>
        {
            if (repo.FirstOrDefault(entity.Id) == null)
            {
                return repo.Insert(entity);
            }
            return entity;
        }
    }

    internal static class StringExtensions
    {
        public static string GetMd5Id(this string myString)
        {
            using (var md5 = new MD5CryptoServiceProvider())
            {
                var fromData = System.Text.Encoding.Unicode.GetBytes(myString);
                var targetData = md5.ComputeHash(fromData);
                var builder = new System.Text.StringBuilder();
                foreach (var t in targetData)
                {
                    builder.Append(t.ToString("x"));
                }
                var byte2String = builder.ToString();
                return byte2String;
            }
        }
    }
}
